#!/bin/bash
set -e

while getopts  ":s:p:t:w:d:h" opts
do
        case  $opts  in
		s) sample__name=$OPTARG;;
		p) out_prefix=$OPTARG;;
		t) threads=$OPTARG;;
		w) win=$OPTARG;;
		d) fa_dir=$OPTARG;;
		h)
		echo `basename $0` '[-s sample_name] [-d fasta_dir] [-w window_size] [-p out_prefix] <bam>'
		exit 1
		;;
		\?)
		echo `basename $0` ': invalid option.'
		exit 1
		;;
		esac
done
shift $(($OPTIND - 1))

if [ -z "$1" ]; then
	echo `basename $0` '[-s sample_name] [-d fasta_dir] [-w window_size] [-p out_prefix] <bam>'
	exit 1
fi


. /mnt/ilustre/app/medical/tools/.var

if test -z $win;then
win=200000
fi
echo $win

if test -z $fa_dir;then
fa_dir=$data_path/ref/b38
fi
echo $fa_dir |grep '.fa$'

chroms="$(seq 22) X Y"
echo chroms: $chroms

cnvnator -root $out_prefix.root -tree $1 -unique

# cnvnator -root $out_prefix.root  -genome $data_path/ref/b38/b38.fa -chrom $chroms -merge file1.root ...

cnvnator -root $out_prefix.root -chrom $chroms -d $fa_dir -his $win

cnvnator -root $out_prefix.root -chrom $chroms -stat $win # before partition and cnv calling

cnvnator -root $out_prefix.root -chrom $chroms -partition $win

cnvnator -root $out_prefix.root -chrom $chroms -call $win > $out_prefix.cnv.cnvnator.txt


# call result info
# normalized_RD -- normalized to 1.
# e-val1        -- is calculated using t-test statistics.
# e-val2        -- is from the probability of RD values within the region to be in
# the tails of a gaussian distribution describing frequencies of RD values in bins.
# e-val3        -- same as e-val1 but for the middle of CNV
# e-val4        -- same as e-val2 but for the middle of CNV
# q0            -- fraction of reads mapped with q0 quality


# cnvnator -root $out_prefix.root -eval $win


# cnvnator -root $out_prefix.root -genotype $win
# cnvnator -root $out_prefix.root -view $win -ngc
# cnvnator -pe   file1.bam ... -qual val(20) -over val(0.8) -f file


# cnvnator -root $out_prefix.root -tree 1.0.1x.bam

# cnvnator -root $out_prefix.root -his 200 -d $data_path/ref/b38 -chrom 7 8

# cnvnator -root $out_prefix.root -stat 200 -chrom 7 8

# cnvnator -root $out_prefix.root -partition 200 -chrom 7 8

# cnvnator -root $out_prefix.root -call 200 -chrom 7 8

